package com.heima.wemedia.config.RabbitConfig;

import com.heima.common.constants.message.NewsUpOrDownConstants;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DeclareUpOrDownRabbitConfig {
    /**
     * 延时交换机
     * RabbitMQ需要安装延迟插件
     */
    @Bean
    public TopicExchange newsUpOrDownTopicExchange(){
        return new TopicExchange(NewsUpOrDownConstants.NEWS_UP_OR_DOWN_EXCHANGE, true,false);
    }
    /**
     * 声明文章上架队列
     * @return
     */
    @Bean
    public Queue upArticleQueue(){
        return new Queue(NewsUpOrDownConstants.NEWS_UP_FOR_ARTICLE_CONFIG_QUEUE, true);
    }
    /**
     * 声明文章下架队列
     * @return
     */
    @Bean
    public Queue downArticleQueue(){
        return new Queue(NewsUpOrDownConstants.NEWS_DOWN_FOR_ARTICLE_CONFIG_QUEUE, true);
    }
    /**
     * 绑定 交换机 + 文章上架队列
     * @return
     */
    @Bean
    public Binding bindingNewsForUpArticle(){
        return BindingBuilder.bind(upArticleQueue()).to(newsUpOrDownTopicExchange()).with(NewsUpOrDownConstants.NEWS_UP_ROUTE_KEY);
    }
    /**
     * 绑定 交换机 + 文章下架队列
     * @return
     */
    @Bean
    public Binding bindingNewsForDownArticle(){
        return BindingBuilder.bind(downArticleQueue()).to(newsUpOrDownTopicExchange()).with(NewsUpOrDownConstants.NEWS_DOWN_ROUTE_KEY);
    }
}
